package com.bwie.service.impl;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.lang.tree.Tree;
import cn.hutool.core.lang.tree.TreeUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bwie.pojo.TbType;
import com.bwie.service.TbTypeService;
import com.bwie.mapper.TbTypeMapper;
import com.bwie.utils.Result;
import com.bwie.vo.PageInfoVo;
import com.github.tobato.fastdfs.domain.StorePath;
import com.github.tobato.fastdfs.service.FastFileStorageClient;
import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jwts;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.Date;
import java.util.List;

/**
* @author 28242
* @description 针对表【tb_type(商品分类表)】的数据库操作Service实现
* @createDate 2025-05-24 10:11:58
*/
@Service
public class TbTypeServiceImpl extends ServiceImpl<TbTypeMapper, TbType>
    implements TbTypeService{


   @Autowired
    FastFileStorageClient fastFileStorageClient;


    @Autowired
    TbTypeMapper tbTypeMapper;

    @Autowired
    HttpServletRequest request;

    @Override
    public Result addType(TbType tbType) {


        String token = request.getHeader("token");
        if (token==null){
            return Result.error().setMsg("请先登录");
        }
        Claims body = Jwts.parser().setSigningKey("123456").parseClaimsJws(token).getBody();
        String nickName = body.get("nickName", String.class);

        tbType.setCreateBy(nickName);
        tbType.setCreateTime(new Date());


        tbTypeMapper.insert(tbType);

        return Result.ok();
    }

    @Override
    public Result deleteType(Integer typeId) {

        //判断商品是否存在
        TbType tbType = tbTypeMapper.selectById(typeId);
        if (tbType==null){
            return Result.error().setMsg("分类不存在");
        }


        tbTypeMapper.deleteById(typeId);


        return Result.ok();
    }

    @Override
    public Result getTypeList() {
        List<TbType> list = tbTypeMapper.selectList(
                null
        );
        List<Tree<String>> treeList = TreeUtil.build(list, "0", (menu, tree) -> {
            tree.setId(menu.getTypeId() + "");
            tree.setName(menu.getTypeName());
            tree.setParentId(menu.getTypePid() + "");

            tree.putExtra("label",menu.getTypeName());
            tree.putExtra("value",menu.getTypeId());
            tree.putExtra("typeImage", menu.getTypeImage());
            tree.putExtra("typeStatus", menu.getTypeStatus());
            tree.putExtra("typeTop", menu.getTypeTop());
            tree.putExtra("createTime",menu.getCreateTime());
        });

        return Result.ok().setData(treeList);
    }

    @Override
    public Result changeTypeStatus(Integer typeId, Integer status) {

        QueryWrapper<TbType> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(TbType::getTypeId,typeId);
        TbType tbType = tbTypeMapper.selectOne(wrapper);
        if (tbType==null){
            return Result.error().setMsg("该类型不存在");
        }

        tbType.setTypeStatus(status);
        tbTypeMapper.updateById(tbType);


        return Result.ok();
    }

    @Override
    public Result deleteTypeById(Integer typeId) {

        //获取该分类信息

        TbType tbType = tbTypeMapper.selectById(typeId);
        if (tbType==null){
            return Result.error().setMsg("该类型不存在");
        }


        //根据id删除该分类

        tbTypeMapper.deleteById(typeId);


        return Result.ok();
    }

    @Override
    public Result uploadeImage(MultipartFile file) {

        String extName = FileUtil.extName(file.getOriginalFilename());
        try {
            StorePath storePath = fastFileStorageClient.uploadFile(file.getInputStream(), file.getSize(), extName, null);

            String url="http://192.168.80.192:8888/"+storePath.getFullPath();

            return Result.ok().setData(url);
        } catch (IOException e) {
            e.printStackTrace();
           return Result.error().setMsg("上传失败");
        }
    }

    @Override
    public Result updateType(TbType tbType) {

        TbType type = tbTypeMapper.selectById(tbType.getTypeId());
        if (type==null){
            return Result.error().setMsg("分类不存在");
        }

        tbType.setTypePid(tbType.getTypePid());

        tbTypeMapper.updateById(tbType);


        return Result.ok();
    }


}




